class Solution {
public:
    int countPairs(vector<int>& nums, int target) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        int count = 0;
        int j = n - 1;
        for(int i = 0; i < n - 1; ++i)
        {
            while(j > i && nums[i] + nums[j] >= target) --j;
            count += j - i;
            j = n - 1;
        }
        return count;
    }
};
